import axios, {AxiosResponse} from "axios";

axios.defaults.baseURL = 'http://oss-cloud.meblog.online:8088/oss-cloud';
// axios.defaults.baseURL = 'http://localhost:9999/oss-cloud';
axios.defaults.headers.get['Content-Type'] = 'application/json';
axios.defaults.withCredentials = true


axios.interceptors.request.use(
    config => {
        if (config.url?.startsWith("/api")) {
            config.headers = {
                "Authorization": "Oss " + localStorage.getItem("token")
            }
            // config.headers['Authorization'] = "oss " + localStorage.getItem("token");
        }
        return config;
    },
    error => {
        return Promise.reject(error)
    }
)


axios.interceptors.response.use(
    response => {
        return response
    },
    error => {
        const response = error.response;
        return Promise.reject(response || {message: error.message})
    }
)

interface ResultResp<T> {
    code: string
    msg: string
    data: T
}


export interface PageResult<T> {
    content: T[];
    limit: number;
    page:number;
    total: number;
}


export const HttpGet = async <T>(url:string, params?: any) => {
    const {data} = await axios.get<T, AxiosResponse<ResultResp<T>>, any>(url, {params: params})
    console.log("NewHttpGet", data)
    return data
}

export const Post = async <T>(url:string, params: any) => {
    const {data} = await axios.post<T, AxiosResponse<ResultResp<T>>, any>(url, params)
    console.log("NewHttpPost", data)
    return data
}


export const FormPost = async <T>(url:string, params: any) => {
    // , {
    //         headers: {"Content-Type": "application/x-www-form-urlencoded"}
    //     }
    const {data} = await axios.post<T, AxiosResponse<ResultResp<T>>, any>(url, params, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
    console.log("NewHttpPost", data)
    return data
}

// export const HttpGet = <T>(url: string, data: any) => {
//     return new Promise<T>((resolve, reject) => {
//         axios.get(url, {params: data})
//             .then(res => resolve(res.data))
//             .catch(err => reject(err))
//     })
// }



